//
//  LGSportScreenOrderView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/11/6.
//

#import "LGSportScreenOrderView.h"

#define itemHeight  viewPix(32)

@interface LGSportScreenOrderView()
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UIButton *closeBtn;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UILabel *statusTitle;
/**   */
@property (nonatomic , strong)UIView *lineView;
/**   */
@property (nonatomic , strong)UIButton *resetBtn;
/**   */
@property (nonatomic , strong)UIButton *sureBtn;

@end

@implementation LGSportScreenOrderView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0];
        [self addSubview:self.baseView];
        [self.baseView addSubview:self.titleLabel];
        [self.baseView addSubview:self.closeBtn];
        [self.baseView addSubview:self.statusTitle];
        [self.baseView addSubview:self.lineView];
        [self.baseView addSubview:self.resetBtn];
        [self.baseView addSubview:self.sureBtn];
        [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.baseView).offset(-viewPix(100));
            make.left.right.equalTo(self.baseView);
            make.height.equalTo(@(0.5));
        }];
        [self.resetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.lineView.mas_bottom).offset(viewPix(8));
            make.left.equalTo(self.baseView).offset(viewPix(48));
            make.width.equalTo(@(viewPix(128)));
            make.height.equalTo(@(viewPix(46)));
        }];
        [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.baseView).offset(-viewPix(48));
            make.centerY.width.height.equalTo(self.resetBtn);
        }];
        [self performSelector:@selector(showViewAniamtion) withObject:nil afterDelay:0.2];
    }
    return self;
}


-(void)setStatusArray:(NSArray *)statusArray{
    _statusArray = statusArray;
    CGFloat itemWidth = (Screen_W-viewPix(35)-viewPix(24))/3.0;
    for (NSInteger i=0; i<statusArray.count; i++) {
        NSDictionary *tempDic = statusArray[i];
        UIButton *button = [UIButton buttonWithTitle:tempDic[@"title"] titleFont:LGFontWeight(15, UIFontWeightMedium) normalColor:@"#666666" selectColor:@"#FFFFFF"];
        button.frame = CGRectMake(viewPix(20)+(itemWidth+viewPix(8))*(i%3), viewPix(93)+(itemHeight+viewPix(16))*(i/3), itemWidth, itemHeight);
        button.selected = [tempDic[@"title"] isEqualToString:self.status]?YES:NO;
        [button setBackgroundColor:[UIColor colorWithString:@"#F6F6F6"] forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor colorWithString:@"#3291FF"] forState:UIControlStateSelected];
        [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
        button.titleLabel.numberOfLines = 2;
        button.cornerRidus = viewPix(16);
        button.tag = 1000+i;
        [button addTarget:self action:@selector(statusButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.baseView addSubview:button];
        if([LGNSString(tempDic[@"value"]) isEqualToString:self.status]) button.selected = YES;
        if(i == statusArray.count-1){
            self.baseView.frame = CGRectMake(0, Screen_H, Screen_W, kMaxY(button.frame)+viewPix(190));
        }
    }
}


-(void)closeButtonAction{
    CGFloat height = self.baseView.bounds.size.height;
    [UIView animateWithDuration:0.4 animations:^{
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0];
        self.baseView.frame = CGRectMake(0, Screen_H, Screen_W, height);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

//重置
-(void)resetBtnAction{
    if(self.screenOrderResult){
        self.screenOrderResult(@"");
    }
    [self closeButtonAction];
}

//确定
-(void)sureButtonAction{
    if(self.screenOrderResult){
        self.screenOrderResult(self.status);
    }
    [self closeButtonAction];
}



//订单状态
-(void)statusButtonAction:(UIButton *)sender{
    for (NSInteger i=0; i<self.statusArray.count; i++) {
        NSDictionary *tempDic = self.statusArray[i];
        UIButton *button = [self viewWithTag:1000+i];
        if(sender.tag-1000 == i){
            self.status = LGNSString(tempDic[@"value"]);
            button.selected = YES;
        }else{
            button.selected = NO;
        }
    }
}

-(void)showViewAniamtion{
    CGFloat height = self.baseView.bounds.size.height;
    [UIView animateWithDuration:0.4 animations:^{
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0.5];
        self.baseView.frame = CGRectMake(0, Screen_H-height+15, Screen_W, height);
    }];
}



-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc] initWithFrame:CGRectMake(0, Screen_H, Screen_W, viewPix(350))];
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.cornerRidus = 15;
    }
    return _baseView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"订单筛选" colorString:@"#333333" textFont:LGFontWeight(16, UIFontWeightSemibold) textAlignment:NSTextAlignmentCenter lines:1];
        _titleLabel.frame = CGRectMake(viewPix(100), viewPix(12), Screen_W-viewPix(200), viewPix(36));
    }
    return _titleLabel;
}

-(UIButton *)closeBtn{
    if(!_closeBtn){
        _closeBtn = [UIButton buttonWithImage:@"closeGray16" selectImage:@"closeGray16"];
        _closeBtn.frame = CGRectMake(Screen_W-viewPix(46), viewPix(12), viewPix(36), viewPix(36));
        [_closeBtn addTarget:self action:@selector(closeButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}

-(UILabel *)statusTitle{
    if(!_statusTitle){
        _statusTitle = [UILabel lableWithText:@"订单状态" colorString:@"#333333" textFont:LGFontWeight(15, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _statusTitle.frame = CGRectMake(viewPix(20), viewPix(58), viewPix(100), 22);
    }
    return _statusTitle;
}
    

-(UIView *)lineView{
    if(!_lineView){
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [UIColor colorWithString:@"#F2F2F2"];
    }
    return _lineView;
}


-(UIButton *)resetBtn{
    if(!_resetBtn){
        _resetBtn = [UIButton buttonWithTitle:@"重置" titleFont:LGFontWeight(16, UIFontWeightMedium) textColor:@"#4C87FD" imageName:@""];
        _resetBtn.layer.borderColor = [UIColor colorWithString:@"#4C87FD"].CGColor;
        _resetBtn.layer.borderWidth = 1;
        _resetBtn.cornerRidus = 10;
        [_resetBtn addTarget:self action:@selector(resetBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _resetBtn;
}

-(UIButton *)sureBtn{
    if(!_sureBtn){
        _sureBtn = [UIButton buttonWithTitle:@"确定" titleFont:LGFontWeight(16, UIFontWeightMedium) textColor:@"#FFFFFF" imageName:@""];
        _sureBtn.backgroundColor = [UIColor colorWithString:@"#4C87FD"];
        _sureBtn.cornerRidus = 10;
        [_sureBtn addTarget:self action:@selector(sureButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}







@end
